import cv2
import numpy as np

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread('C:/Users/nic/Desktop/opencv/picture/duo.jpg')

#先把图像转化为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#再把灰度图转换为二值图
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
#contours是轮廓信息，hierarchy是结构层次
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#绘制轮廓
img_same = img.copy()
res = cv2.drawContours(img_same,contours,-1,(0,0,255),2)
#一起展示
res = np.hstack((res,img))
cv_show('all',res)


